var app = getApp();
var util = require('../../../../utils/util.js');
var api = require('../../../../config/api.js');
var lastTime = null;
Page({
  data: {
    fenxiaoshang: {},
    opentixian: false,
    tixianyongjin: 0
  },
  onLoad: function (options) {
    let that = this;
    util.request(api.Fenxiaozhongxininfo).then(function (res) {
      if (res.result == 1) {
        that.setData({
          fenxiaoshang: res.data
        });
      }
    }).catch((err) => {
      reject(err);
    });
  },
  onReady: function () {
    // 页面渲染完成
  },
  onShow: function () {

  },
  onHide: function () {
    // 页面隐藏

  },
  onUnload: function () {
    // 页面关闭
  },
  //分销订单
  gofenxiaoorder: function () {
    wx.navigateTo({
      url: "/pages/ucenter/fenxiao/fenxiaoorder/fenxiaoorder"
    });
  },
  //分销佣金
  gofenxiaoyongjin: function () {
    wx.navigateTo({
      url: "/pages/ucenter/fenxiao/fenxiaoyongjin/fenxiaoyongjin"
    });
  },
  //提现明细
  gofenxiaotixianmingxi: function () {
    wx.navigateTo({
      url: "/pages/ucenter/fenxiao/fenxiaotixianmingxi/fenxiaotixianmingxi"
    });
  },
  //下级分销商
  goxiajifenxiaoshang: function () {
    wx.navigateTo({
      url: "/pages/ucenter/fenxiao/fenxiaoshang/myfenxiaoshang/myfenxiaoshang"
    });
  },
  //分销商申请
  gofenxiaoshangshenqing: function () {
    wx.navigateTo({
      url: "/pages/ucenter/fenxiao/fenxiaoshangshenqing/fenxiaoshangshenqing"
    });
  },
  //佣金提现申请
  goyongjintixian: function () {
    let that = this;
    if (that.data.fenxiaoshang.ketixian > 0) {
      that.setData({
        opentixian: true
      })
    } else {
      wx.showToast({
        title: '暂无可提现佣金！',
        icon: 'none'
      })
    }
  },
  //点击确定按钮
  goqueding: function () {
    let that = this;
    if (that.data.tixianyongjin < that.data.fenxiaoshang.zuiditixian) {
      wx.showToast({
        title: '提现金额不符合要求！',
        icon: 'none'
      })
      return false;
    }
    let nowTime = +new Date();
    if (nowTime - lastTime > 5000 || !lastTime) { //5秒内避免重复提交订单
      lastTime = nowTime;
    } else {
      return false;
    }
    wx.showModal({
      title: '提示',
      content: '您确定要提现吗？',
      success: function (sm) {
        if (sm.confirm) {
          util.request(api.Fenxiaoyongjintixian, {
            tixianyongjin: that.data.tixianyongjin
          }, 'POST').then(function (res) {
            if (res.result == 1) {
              that.setData({
                opentixian: false
              })
            }
          }).catch((err) => {
            reject(err);
          });
        } else if (sm.cancel) {
          console.log('用户点击取消')
        }
      }
    });
  },
  //点击取消按钮
  goquxiao: function () {
    let that = this;
    that.setData({
      opentixian: false
    })
  },
  //输入佣金
  bindtixianyongjinInput: function (e) {
    let that = this;
    let price = e.detail.value;
    price = price.replace(/[^\d.]/g, ""); //清除“数字”和“.”以外的字符
    price = price.replace(/\.{2,}/g, "."); //只保留第一个. 清除多余的
    price = price.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
    price = price.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3'); //只能输入两个小数
    if (price.indexOf(".") < 0 && price != "") { //以上已经过滤，此处控制的是如果没有小数点，首位不能为类似于 01、02的金额
      price = parseFloat(price);
    }
    if (price > that.data.fenxiaoshang.ketixian) {
      that.setData({
        tixianyongjin: that.data.fenxiaoshang.ketixian
      });
    } else {
      that.setData({
        tixianyongjin: price
      });
    }
  },
  //点击推广整站
  gotuiguang: function () {
    wx.navigateTo({
      url: "/pages/ucenter/fenxiao/tuiguang/tuiguangzhengzhan/tuiguangzhengzhan"
    });
  },
  //点击推广商品
  gotuiguangshangpin: function () {
    wx.navigateTo({
      url: "/pages/ucenter/fenxiao/tuiguang/tuiguangshangpin/tuiguangshangpin"
    });
  },
})